package cn.tongji.edu.tjmobile;

import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import cn.tongji.edu.tjmobile.component.HelveticaNeueUltraLightView;

public class BusTimelineSelectionActivity extends Activity {

    private String startCampus;
    private String endCampus;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bus_timeline_selection);

        TextView backView = (TextView) findViewById(R.id.back_view);
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread () {
                    public void run () {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch(Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });

        Intent intent = getIntent();
        String routeId = intent.getStringExtra("routeId");
        startCampus = intent.getStringExtra("start");
        endCampus = intent.getStringExtra("end");

        TextView startCampusTextView = (TextView) findViewById(R.id.start_campus_textview);
        startCampusTextView.setText(startCampus);
        TextView endCampusTextView = (TextView) findViewById(R.id.end_campus_textview);
        endCampusTextView.setText(endCampus);

        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
        Calendar cal = Calendar.getInstance();
        TimeZone timeZone = cal.getTimeZone();
        cal.add(Calendar.DAY_OF_MONTH, 1);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);
        int weekOfDay = cal.get(Calendar.DAY_OF_WEEK);
        String weekOfDayString = "";
        switch (weekOfDay) {
            case 1:
                weekOfDayString = "日";
                break;
            case 2:
                weekOfDayString = "一";
                break;
            case 3:
                weekOfDayString = "二";
                break;
            case 4:
                weekOfDayString = "三";
                break;
            case 5:
                weekOfDayString = "四";
                break;
            case 6:
                weekOfDayString = "五";
                break;
            case 7:
                weekOfDayString = "六";
                break;
        }
        String currentDate = year + "年" + month + "月" + day + "日 " + "星期" + weekOfDayString;

        TextView currentDateView = (TextView) findViewById(R.id.current_date_textview);
        currentDateView.setText(currentDate);

        GetBusInfo getBusInfoTask = new GetBusInfo();
        getBusInfoTask.execute(routeId);
    }

    class GetBusInfo extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... params) {
            HttpClient client = new DefaultHttpClient();
            String url = "http://jiading.tongji.edu.cn:8080/TJbus/GetBusServlet?route_id=" + params[0] + "&admin=0";
            HttpGet request = new HttpGet(url);
            try {
                HttpResponse response = client.execute(request);
                String content = IOUtils.toString(response.getEntity().getContent());
                response.getEntity().consumeContent();
                JSONArray jsonArray = new JSONArray(content);
                LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

                LinearLayout timeLineContainerLayout = (LinearLayout) findViewById(R.id.time_line_container_layout);
                for (int i = 0; i != jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    final String time = jsonObject.getString("time");
                    String line = jsonObject.getString("line");
                    int rest = jsonObject.getInt("rest");
                    final String busId = jsonObject.getString("bus_id");
                    View timeLineComponent = inflater.inflate(R.layout.component_time_line, null);
                    timeLineContainerLayout.addView(timeLineComponent);
                    HelveticaNeueUltraLightView timeLineView = (HelveticaNeueUltraLightView) timeLineComponent.findViewById(R.id.time_line_view);
                    TextView lineDescriptionView = (TextView) timeLineComponent.findViewById(R.id.line_description_view);
                    timeLineView.setText(time);
                    if (rest == 0) {
                        timeLineView.setTextColor(Color.parseColor("#ff71cdfd"));
                        lineDescriptionView.setText("预约已满");
                        lineDescriptionView.setTextColor(Color.parseColor("#ffbfbfbf"));
                    } else {
                        lineDescriptionView.setText(line);
                        RelativeLayout clockLayout = (RelativeLayout) timeLineComponent.findViewById(R.id.clock_layout);
                        clockLayout.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent();
                                intent.putExtra("start", startCampus);
                                intent.putExtra("end", endCampus);
                                intent.putExtra("time", time);
                                intent.putExtra("busId", busId);
                                intent.setClass(BusTimelineSelectionActivity.this, QRCodeReserveConfirmActivity.class);
                                startActivity(intent);
                            }
                        });
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}
